/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UsedFeaturesPanel.java
 *
 * Created on 28.09.2010, 13:13:12
 */

package rosedb.gui;

import com.explodingpixels.macwidgets.BottomBar;
import com.explodingpixels.macwidgets.BottomBarSize;
import com.explodingpixels.macwidgets.HudWidgetFactory;
import com.explodingpixels.macwidgets.HudWindow;
import com.explodingpixels.macwidgets.MacWidgetFactory;
import com.explodingpixels.macwidgets.SourceList;
import com.explodingpixels.macwidgets.SourceListCategory;
import com.explodingpixels.macwidgets.SourceListClickListener;
import com.explodingpixels.macwidgets.SourceListControlBar;
import com.explodingpixels.macwidgets.SourceListDarkColorScheme;
import com.explodingpixels.macwidgets.SourceListItem;
import com.explodingpixels.macwidgets.SourceListModel;
import com.explodingpixels.macwidgets.SourceListSelectionListener;
import com.explodingpixels.macwidgets.plaf.HudButtonUI;
import com.explodingpixels.macwidgets.plaf.HudPaintingUtils;
import com.explodingpixels.macwidgets.plaf.HudTextFieldUI;
import com.explodingpixels.widgets.PopupMenuCustomizer;
import com.explodingpixels.widgets.PopupMenuCustomizerUsingStrings;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ConcurrentModificationException;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.tree.DefaultTreeCellEditor.DefaultTextField;
import rosedb.control.Controller;

/**
 *
 * @author riu
 */
public class UsedFeaturesPanel extends javax.swing.JPanel {

    
    private Controller.Data data;
    private Controller control;
    private String pickedFeature = null;
    
    private JTextField txt_newFeat;
    private JButton btn_add;
    private SourceList sourceList;
    private SourceListCategory main;
    private SourceListCategory search;
    
    List<String> hide;

    /** Creates new form UsedFeaturesPanel */
    public UsedFeaturesPanel(Controller.Data d, Controller c, List<String> h) {
        initComponents();
        setOpaque(false);
        

        //btn_ok.setEnabled(false);
        hide = h;
        control = c;
        data = d;

        //li_features.setModel(list);

        initMacWidgets();

        txt_newFeat.setBackground(Color.darkGray);
        txt_newFeat.setForeground(HudPaintingUtils.FONT_COLOR);

        txt_newFeat.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(HudPaintingUtils.BORDER_COLOR),
                BorderFactory.createEmptyBorder(1, 2, 1, 2)));

        txt_newFeat.setSelectedTextColor(Color.black);
        txt_newFeat.setSelectionColor(HudPaintingUtils.FONT_COLOR);
        txt_newFeat.setCaretColor(HudPaintingUtils.FONT_COLOR);

        txt_newFeat.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar() == '\n'){
                    if(search != null && search.getItemCount()>0){
                        pickedFeature = search.getItems().get(0).getText();
                        UsedFeaturesPanel.this.getParent().getParent().getParent().getParent().setVisible(false);
                    }
                }
            }

        });

        sourceList.useIAppStyleScrollBars();
        
        
       

        sourceList.addSourceListSelectionListener(new SourceListSelectionListener() {

            @Override
            public void sourceListItemSelected(SourceListItem sli) {
                if(sli != null){
                    
                }
            }
        });

        sourceList.addSourceListClickListener(new SourceListClickListener() {

            @Override
            public void sourceListItemClicked(SourceListItem sli, Button button, int i) {
                if(i>1 && sourceList.getSelectedItem()!=null && button == Button.LEFT){
                    pickedFeature = sourceList.getSelectedItem().getText();
                    UsedFeaturesPanel.this.getParent().getParent().getParent().getParent().setVisible(false);
                }
            }

            @Override
            public void sourceListCategoryClicked(SourceListCategory slc, Button button, int i) {

            }
        });

        btn_add.addActionListener(
            new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    //String newFeature = JOptionPane.showInputDialog(UsedFeaturesPanel.this.getParent(), "Neue Eigenschaft");
                    String newFeature = txt_newFeat.getText().trim();
                    if(newFeature != null && !newFeature.equals("") && !isAlreadyInFeatures(newFeature) && !newFeature.equalsIgnoreCase("name")){
                        SourceListItem sli;
                        sourceList.getModel().addItemToCategory(sli = new SourceListItem(newFeature), sourceList.getModel().getCategories().get(0));
                        data.getFeatures().add(newFeature);
                        control.saveData();
                        //pickedFeature = newFeature;
                        sourceList.setSelectedItem(sli);
                    }else if(newFeature != null && data.getFeatures().contains(newFeature.toLowerCase().trim())){
                        pickedFeature = newFeature;
                        UsedFeaturesPanel.this.getParent().getParent().getParent().getParent().setVisible(false);
                    }
                }
        });

        txt_newFeat.addCaretListener(new CaretListener() {

            @Override
            public synchronized void caretUpdate(CaretEvent e) {
                if(!txt_newFeat.getText().trim().equals("")){

                    
                    sourceList.getModel().removeCategory(search);
                    search = new SourceListCategory("Filter");
                    sourceList.getModel().addCategory(search);


                    for(SourceListItem i : main.getItems()){
                        if(i.getText().trim().toLowerCase().contains(txt_newFeat.getText().trim().toLowerCase())){
                            sourceList.getModel().addItemToCategory(new SourceListItem(i.getText()), search);
                        }
                    }

                    sourceList.setExpanded(main, false);
                    sourceList.setExpanded(search, true);
                }else{
                    sourceList.setExpanded(main, true);
                    sourceList.setExpanded(search, false);
                }
            }
        });
    }

    private boolean isAlreadyInFeatures(String feat){
        for(String s : data.getFeatures()){
            if(s.trim().equalsIgnoreCase(feat.trim())){
                return true;
            }
        }

        return false;
    }

    private void initMacWidgets(){
        setLayout(new BorderLayout());

        SourceListModel model = new SourceListModel();
        main = new SourceListCategory("Bisherige Eigenschaften");
        model.addCategory(main);
        
        for(String s : data.getFeatures()){
            if(!hide.contains(s)){
                model.addItemToCategory(new SourceListItem(s), main);
            }
        }
        
        search = new SourceListCategory("Filter");
        model.addCategory(search);

        
        sourceList = new SourceList(model);
        sourceList.setColorScheme(new SourceListDarkColorScheme());
        add(sourceList.getComponent(), BorderLayout.CENTER);

        //BottomBar controls = new BottomBar(BottomBarSize.LARGE);
        JPanel controls = new JPanel();
        controls.setOpaque(false);
        controls.setLayout(new BorderLayout(5, 1));
        //controls.addComponentToRight(MacWidgetFactory.createEmphasizedLabel(model.getCategories().get(0).getItemCount()+" Eigenschaften"));

        controls.add(btn_add = HudWidgetFactory.createHudButton("+"), BorderLayout.WEST);
        //controls.add(btn_ok = HudWidgetFactory.createHudButton("OK"), BorderLayout.CENTER);
        controls.add(txt_newFeat = new JTextField(), BorderLayout.CENTER);
        add(controls, BorderLayout.SOUTH);
    }


    public String getPickedFeature() {
        return pickedFeature;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 267, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 407, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables



}
